//
//  LoginViewController.m
//  LoveDraw
//
//  Created by 零零壹 on 2017/7/8.
//  Copyright © 2017年 梁晨. All rights reserved.
//

#import "LoginViewController.h"
#import "NSString+MD5Addition.h"
#import "JPUSHService.h"

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *PhoneNumTF;
@property (weak, nonatomic) IBOutlet UITextField *PassWorldTF;
@property (weak, nonatomic) IBOutlet UIView *PhoneV;
@property (weak, nonatomic) IBOutlet UIView *PassWorldV;
@property (weak, nonatomic) IBOutlet UIButton *LoginV;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setLayout];

}

/**
 界面效果
 */
-(void)setLayout
{
    self.PhoneV.layer.masksToBounds = YES;
    self.PhoneV.layer.cornerRadius = 5;
    self.PhoneV.layer.borderWidth = 1;
    self.PhoneV.layer.borderColor = UIColors(0xDFDFDF).CGColor;

    self.PassWorldV.layer.masksToBounds = YES;
    self.PassWorldV.layer.cornerRadius = 5;
    self.PassWorldV.layer.borderWidth = 1;
    self.PassWorldV.layer.borderColor = UIColors(0xDFDFDF).CGColor;
    self.LoginV.layer.masksToBounds = YES;
    self.LoginV.layer.cornerRadius = 5;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userTextFieldDidChange) name:UITextFieldTextDidChangeNotification object:self.PhoneNumTF];

}

// 观察者模式
-(void)userTextFieldDidChange
{
    if (self.PhoneNumTF.text.length>0) {
        [self.LoginV setTitleColor:UIColors(0xFFFFFF) forState:(UIControlStateNormal)];
        self.LoginV.userInteractionEnabled = YES;

    }else
    {
        [self.LoginV setTitleColor:PKCOLOR(200, 200, 200) forState:(UIControlStateNormal)];
        self.LoginV.userInteractionEnabled = NO;
    }
}

- (IBAction)LoginAct:(id)sender
{
    if (self.PassWorldTF.text.length == 0) {
        [HUD showText:@"请输入密码" AfterTime:1];
        return;
    }

    NSString *uuid = [DeviceInfo getUUid];

    [RequestManager HudRequestWithUrl:@"/index.php?g=App&m=Login&a=login" parmDic:@{@"username":self.PhoneNumTF.text,@"password":[self.PassWorldTF.text stringFromMD5],@"UUID":uuid} requestType:RequestPOST view:self.view Success:^(id data)
    {
        
        UserInfo *userinfo = [UserInfo mj_objectWithKeyValues:data];
        [UserInfoManage sharedInstance].userInfo = userinfo;

        UIWindow *window = [UIApplication sharedApplication].windows[0];
        UIViewController *root = [[NSClassFromString(@"RootViewViewController") alloc]init];
        window.rootViewController = root;

        NSSet *set = [NSSet setWithObjects:[NSString stringWithFormat:@"s_%@",[UserInfoManage sharedInstance].userInfo.suid],[NSString stringWithFormat:@"m_%@",[UserInfoManage sharedInstance].userInfo.maid], nil];

        [JPUSHService setAlias:[UserInfoManage sharedInstance].userInfo.uid callbackSelector:nil object:nil];

        [JPUSHService addTags:set completion:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {

        } seq:0];

        NSLog(@"%@",[UserInfoManage sharedInstance].userInfo.uid);
        NSLog(@"%@",[UserInfoManage sharedInstance].userInfo.suid);
        NSLog(@"%@",[UserInfoManage sharedInstance].userInfo.maid);
    } error:^(NSError *error) {

    }];

//    [RequestManager ProgressRequestWithUrl:@"/index.php?g=App&m=Login&a=login" parmDic:@{@"username":self.PhoneNumTF.text,@"password":[self.PassWorldTF.text stringFromMD5],@"UUID":uuid} requestType:RequestPOST view:self.view Success:^(id data)
//    {
//
//        UserInfo *userinfo = [UserInfo mj_objectWithKeyValues:data];
//        [UserInfoManage sharedInstance].userInfo = userinfo;
//
//        UIWindow *window = [UIApplication sharedApplication].windows[0];
//          UIViewController *root = [[NSClassFromString(@"RootViewViewController") alloc]init];
//           window.rootViewController = root;
//
//        NSSet *set = [NSSet setWithObjects:[NSString stringWithFormat:@"s_%@",[UserInfoManage sharedInstance].userInfo.suid],[NSString stringWithFormat:@"m_%@",[UserInfoManage sharedInstance].userInfo.maid], nil];
//
//        [JPUSHService setAlias:[UserInfoManage sharedInstance].userInfo.uid callbackSelector:nil object:nil];
//
//        [JPUSHService addTags:set completion:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
//
//        } seq:0];
//
//        NSLog(@"%@",[UserInfoManage sharedInstance].userInfo.uid);
//        NSLog(@"%@",[UserInfoManage sharedInstance].userInfo.suid);
//        NSLog(@"%@",[UserInfoManage sharedInstance].userInfo.maid);
//
//    } error:^(NSError *error) {
//
//    }];

}
- (IBAction)PresentMessageLogin:(id)sender
{
    UIViewController *vc = [[NSClassFromString(@"MessageLoginViewController") alloc]init];
    vc.modalTransitionStyle = UIModalPresentationPageSheet;
    [self presentViewController:vc animated:YES completion:nil];
}

- (IBAction)forgetPassWorld:(id)sender
{
    UIViewController *vc = [[NSClassFromString(@"forgetPassWorldViewController") alloc]init];
    vc.modalTransitionStyle = UIModalPresentationPageSheet;
    [self presentViewController:vc animated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
